package com.examplemartin.jpademo.service;

import com.examplemartin.jpademo.dao.GoodDao;
import com.examplemartin.jpademo.pojo.po.good.GoodPO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GoodService {

    @Resource
    private GoodDao goodDao;

    // 返回供应商列表
    public List<GoodPO> findAll() {
        return goodDao.findAll();
    }

    public List<GoodPO> findAllByStatus(Integer status) {
        return goodDao.findAllByStatus(status);
    }

    // 按id检索
    public GoodPO findById(Long id) {
        GoodPO goodPO = goodDao.findById(id).get();
        return goodPO;
    }

    // 按供应商名称检索 -- 用于 01 添加供应商时 检测是否重名
    public List<GoodPO> findAllByName(String name) {
        return goodDao.findAllByName(name);
    }

    // 查重复 商品名+商品规格
    public List<GoodPO> findByNameAndSpec(String name , String spec) {
        return goodDao.findByNameAndSpec(name,spec);
    }

    // 保存供应商
    public GoodPO save(GoodPO goodPO) {
        return goodDao.save(goodPO);
    }

    // 计算总条目
    public long countAll() {
        long count = goodDao.count();
        return count;
    }

    // 查询分页
    public List<GoodPO> findAllPage(Pageable page) {
        if (page == null) {
            page = PageRequest.of(0, 10);
        }

        Page<GoodPO> all = goodDao.findAll(page);
        List<GoodPO> content = all.getContent();
        return content;
    }
}
